package com.cyzy.controller;

import com.cyzy.dto.ResponseDto;
import com.cyzy.pojo.UserCoupons;
import com.cyzy.service.UserCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/userCoupon")
public class UserCouponController {

    @Autowired
    private UserCouponsService userCouponsService;

    @GetMapping("/selectMyCoupon")
    public ResponseDto selectMyCoupon(@RequestParam("userId") long userId){

        System.out.println(userId+"---------userId----------");

        List<UserCoupons> userCoupons = userCouponsService.selectMyCoupon(userId);
        if (userCoupons== null){
            return ResponseDto.fail("获取优惠卷信息失败");
        }
        return ResponseDto.ok(userCoupons);
    }

    @PostMapping("/getCoupon")
    public ResponseDto insertUserCoupon(@RequestBody UserCoupons userCoupons){
        System.out.println(userCoupons+"---------------insertUserCoupon--------------");
        //获取当前时间
        Timestamp now = new Timestamp(System.currentTimeMillis());
        // 计算毫秒数（1天=24*60*60*1000毫秒）
        long millisecondsToAdd = userCoupons.getValidDays() * 24L * 60 * 60 * 1000;
        Timestamp newTimestamp = new Timestamp(now.getTime() + millisecondsToAdd);
        //存入当前时间
        userCoupons.setReceiveTime(now);
        //存入过期时间
        userCoupons.setExpireTime(newTimestamp);
        boolean save = userCouponsService.save(userCoupons);
        if(!save){
            return ResponseDto.fail();
        }
        return ResponseDto.ok("领取成功！");
    }

}
